package webappcontrollers
//安全帽设备管理相关功能

import (
	"github.com/astaxie/beego"
	"smartcap-api/models"
	"fmt"
)

type WebappControllers struct {
	beego.Controller
}


//添加设备信息
func (c *WebappControllers) AddDeviceInfo() {

	deviceid := c.GetString("deviceid")
	username := c.GetString("username")

	devicedata := models.LabelInfo{Labelid:deviceid,Labeluser:username,Online:"1",Badhandstatus:"null",Wearingstatus:"1",Electricquantity:"null",Networkstatus:"null"}
	id,err:= models.AddDevice(&devicedata)

	type DATA struct {
		Id int64
	}
	type JSONS struct {
		//必须的大写开头
		Error_code int
		DATA DATA
	}
   if err != nil{
		data := &JSONS{1,DATA{Id:id}}
		c.Data["json"] = data
		c.ServeJSON()
	}else {
		data := &JSONS{0,DATA{Id:id}}
		c.Data["json"] = data
		c.ServeJSON()
	}
}


//查询所有设备
func (this *WebappControllers) QueryAllDeviceInfo() {

	username := this.GetString("username")
	company := this.GetString("company")

	//fmt.Println(subject)
	//var allslice = []string{}
	labelDataChan := make(chan string, 100000)
	models.ReadLabel(labelDataChan,username,company)
	//	//遍历信道，读取数据
	var i int = 0
	//var m map[string]interface{}    //声明变量，不分配内存
	//m = make(map[string]interface{}) //必不可少，分配内存
	//
	var lenlabelchan = len(labelDataChan)
	equdata := []string{}
	for item := range labelDataChan{
		fmt.Println("get data from database:",item)
		//m[strconv.Itoa(i)] =item
		equdata = append(equdata, item)
		if i == (lenlabelchan - 1) {
			break
		}
		i++
	}

	//this.Data["json"] = m
	//this.ServeJSON()
	//this.StopRun()

	type DATA struct {
		Info []string `json:"data_info"`//key重命名,最外面是反引号
	}
	type JSONS struct {
		//必须的大写开头
		Error_code int
		DATA DATA
	}


	data := &JSONS{0,DATA{Info:equdata}}
	this.Data["json"] = data
	this.ServeJSON()
}












